<?php

namespace app\models;


use app\common\services\ShortMessageServices;
/**
 * This is the model class for table "user".
 *
 * @property int $id
 * @property string $user_name
 *
 * @property string $user_pwd
 * @property string $user_ID
 * @property int $user_role
 * @property int $update_time
 * @property int $create_time
 */
class User extends \yii\db\ActiveRecord {
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_name', 'user_pwd', 'user_ID', 'user_role', 'update_time', 'create_time'], 'required'],
            [['user_role', 'update_time', 'create_time'], 'integer'],
            [['user_name', 'user_pwd', 'user_ID'], 'string', 'max' => 64],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'          => 'ID',
            'user_name'   => 'User Name',
            'user_pwd'    => 'User Pwd',
            'user_ID'     => 'User  ID',
            'user_role'   => 'User Role',
            'update_time' => 'Update Time',
            'create_time' => 'Create Time',
        ];
    }

    public static function getCard($user_ID)
    {
        $user_info = User::find()->where(['user_ID' => $user_ID, 'user_status' => 1])->one();
        if ($user_info) {
            return true;
        }
        return false;
    }

    public static function checkPwd($pwd, $phone)
    {
        $user_info = User::find()->where(['user_phone' => $phone])->one();

        if (!$user_info) {
            return false;
        }
        if ($user_info->user_pwd == $pwd) {
            $short_msg = new ShortMessageServices();
            $short_msg->UseCode($pwd);
            return true;
        }
        if ($user_info->user_pwd != md5($pwd)) {
            return false;
        }
        return true;
    }

}
